﻿<template>
	<el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false" :visible.sync="visible"
		class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
		<el-row :gutter="15" class="">
			<el-form ref="elForm" :model="dataForm" size="small" label-width="150px" label-position="right" :rules="rules">
				<el-col :span="24">
						<el-form-item label="检验方法名称" prop="name">
							<el-input v-model='dataForm.name' placeholder='请输入检验方法' clearable required :style='{"width":"100%"}' maxlength='20' show-word-limit>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
					<el-form-item label="有效标志" prop="enabledMark">
						<el-switch v-model='dataForm.enabledMark' :active-value='1' :inactive-value='0'>
						</el-switch>
					</el-form-item>
				</el-col>
			</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
	import {GetInnspectionMethodInfo,createInnspectionmethod,updateInnspectionmethod} from '@/api/q1/basedata/innspectionmethod'
	export default {
		components: {  },
		props: [],
		data() {
			var validatePass = (rule,value,callback)=>{
            	if(!value || value.trim().length == 0){
                	callback(new Error("请输入检验方法名称"));
            	}else if(value.trim().length > 20){
                	callback(new Error("检验方法长度不能超过20个字符"));
            	}else{
                	callback();
            	}
        	};
			return {
				btnLoading:false,
				loading: false,
				visible: false,
				dataForm: {
					id:'',
					name:undefined,
					enabledMark: 1,
				},
				rules: {
					name:[
					{
                        required: true,
                        validator: validatePass,
                        trigger: '"blur"'
                    },
					],
				},
				typeOptions: [],
				classifyOptions: [],
			}
		},
		computed: {},
        watch: {},
        created() {
		},
		mounted() {
        },
		methods: {
			goBack() {
                this.$emit('refresh')
            },
			init(id) {
				this.dataForm.id = id || 0;
                this.visible = true;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						GetInnspectionMethodInfo(id).then(res =>{
							this.dataForm = res.data;
						})
					}
				});
				// this.$store.commit('generator/UPDATE_RELATION_DATA', {})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
						this.btnLoading = true;
						const formMethod = !this.dataForm.id ? createInnspectionmethod : updateInnspectionmethod;
						formMethod(this.dataForm).then((res) => {
							this.btnLoading = false;
							this.$message({
								message: res.msg,
								type: 'success',
								duration: 1000,
								onClose: () => {
									this.visible = false,
										this.$emit('refresh', true)
								}
							})
						}).catch((res) => {
							this.btnLoading = false;
						})
                    }
                })
			},
		}
	}
</script>
